package de.placidi.pressreview.parsing;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public final class HTMLParserImpl implements HTMLParser {
		
	List<String> links = new ArrayList<String>();
	
	public HTMLParserImpl() {}
	
	public HTMLParserImpl(Boolean proxySet, String proxyHost, String proxyPort) {
		System.setProperty("http.proxySet", proxySet.toString());
		System.setProperty("http.proxyHost", proxyHost);
		System.setProperty("http.proxyPort", proxyPort);
	}
	
	private Document getHTMLDocument(String url) throws IOException {
		Document doc = Jsoup.connect(url).get();
		return doc;
	}
	
	public List<String> getAllTexts(String url, String filter, int depth) throws IOException {
		//List<String> links = new ArrayList<String>();
		links.clear();
		PriorityQueue<Link> pages = new PriorityQueue<Link>();
		List<String> texts = new ArrayList<String>();
		
		pages.offer(new Link(url, 0));
		
		while(pages.size() > 0) {
			System.out.println(pages.size());
			Link link = pages.poll();
			String linkURL = link.getUrl();
			Integer linkDepth = link.getDepth();
			
			try {
				Document doc = this.getHTMLDocument(linkURL);
				
				if (HTMLParserHelper.isEnglish(doc)) {
					texts.add(this.parseText(doc));
					List<String> linksOfThisSite = this.parseLinks(doc, filter);
					
					for (String l : linksOfThisSite) {
						if (linkDepth + 1 <= depth){
								if (!HTMLParserHelper.isContained(links, l)) {
									Link newLink = new Link(l, linkDepth+1);
									pages.offer(newLink);
									links.add(l);
									System.out.println(l + "  ###" +(linkDepth + 1));
								}		
							
						}
					}
				}
					
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		HTMLParserHelper.writeFile(links);
		
		return texts;
	}
	
	
	private List<String> parseLinks(Document htmlDoc, String filter) {
		List<String> linksOfThisSite = new ArrayList<String>();
		
		Elements elements = htmlDoc.select("a[href]");
		for (Element element : elements) {
			String page = element.attr("abs:href");
			if (! HTMLParserHelper.isContained(linksOfThisSite, page)) {
				if (HTMLParserHelper.isDomain(page, filter)  ) {
					 if (HTMLParserHelper.isClean(page)) {
						 linksOfThisSite.add(page);
					 }
				}	
			}
		}
		return linksOfThisSite;	
	}
	
	private String parseText(Document htmlDoc) throws IOException {
		StringBuilder text = new StringBuilder();
		Elements elements = htmlDoc.select("title");
		for (Element element : elements) {
			text.append(element.text());
		}
		return text.toString();
	}
	

}
